package com.kaibes.module.user.safe;

import org.springframework.stereotype.Service;

import com.kaibes.orm.curd.QueryBuilder;
import com.kaibes.orm.curd.UpdateBuilder;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class UserSafeService extends ResponseServiceImpl<UserSafe> {

    public long countByEmail(String email) {
        return count(QueryBuilder.newBuilder().eq("email", email).build());
    }
    
    public UserSafe getByUserId(Integer userId) {
        return get(QueryBuilder.newBuilder().eq("user_id", userId).build());
    }
    
    public UserSafe getByEmail(String email) {
        return get(QueryBuilder.newBuilder().eq("email", email).build());
    }
    
    public UserSafe getUserIdByEmail(String email) {
        return get(QueryBuilder.newBuilder().eq("email", email).build(), "user_id");
    }
    
    public String getEmailByUserId(Integer userId) {
        UserSafe userSafe = get(QueryBuilder.newBuilder().eq("user_id", userId).build(), "email");
        if (userSafe == null) {
            return null;
        } else {
            return userSafe.getEmail();
        }
    }
    
    public boolean updateEmailByUserId(String email, Integer userId) {
        return getMapper().update(UpdateBuilder.newBuilder().setValue("email", email)
                .addQuery(QueryBuilder.newBuilder().eq("user_id", userId).build()).build()) > 0;
    }
    
    public boolean updateByUserId(Object object, Integer userId) {
        return update(object, QueryBuilder.newBuilder().eq("user_id", userId).build(), "user_id", "code") > 0;
    }
    
}
